/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2022 All Rights Reserved.
 */
package com.example.spider.proxy.provider.seofangfa;

import cn.hutool.core.util.ReUtil;
import com.example.spider.proxy.core.ProxyInfo;
import com.example.spider.proxy.provider.AbstractProxyResultHandler;
import com.example.spider.proxy.provider.IpProxySpiderTask;
import com.example.spider.starter.SpiderStarter;
import com.example.spider.task.SpiderTaskQueue;
import org.shoulder.core.converter.LocalDateTimeConverter;

import java.net.Proxy;
import java.util.List;

/**
 * seo 方法 千条随机展示10条，筛选过的，10分钟一次更新 建议4分钟爬一次【只有一页】
 * https://proxy.seofangfa.com/
 *
 * @author lym
 */
//@Component
public class SeoFangFaResultHandler extends AbstractProxyResultHandler implements SpiderStarter {

    public SeoFangFaResultHandler() {
        super("seoFangFa");
    }

    @Override
    public void startSpiders() {
        IpProxySpiderTask task = createTask("https://proxy.seofangfa.com/");
        SpiderTaskQueue.putTask(task);
    }

    @Override
    public String executeCron() {
        return "0 */4 * * * ?";
    }

    /**
     * 处理响应
     */
    @Override
    public void doHandle(IpProxySpiderTask task, String html) {

        String regex = "<tr><td>(.*?)</td>" +
                "<td>(.*?)</td>" +
                "<td>(.*?)</td>" +
                "<td>(.*?)</td>" +
                "<td>(.*?)</td></tr>";

        List<String> ipList = ReUtil.findAll(regex, html, 1);
        List<String> portList = ReUtil.findAll(regex, html, 2);
        List<String> responseTimeList = ReUtil.findAll(regex, html, 3);
        List<String> locationList = ReUtil.findAll(regex, html, 4);
        List<String> lastVerificationTimeList = ReUtil.findAll(regex, html, 5);

        for (int i = 0; i < ipList.size(); i++) {
            ProxyInfo proxyInfo = ProxyInfo.builder()
                    .source(taskName)
                    .ip(ipList.get(i))
                    .port(Integer.parseInt(portList.get(i)))
                    .anonymity("高匿")
                    .type(Proxy.Type.HTTP)
                    .location(locationList.get(i))
                    .responseTime(ProxyInfo.parseSecondToMill(responseTimeList.get(i)))
                    .lastVerificationTime(LocalDateTimeConverter.INSTANCE.convert(lastVerificationTimeList.get(i)))
                    .build();
            // 保存到文件
            addProxyToSourceRepository(proxyInfo);
        }
    }

}